package com.example.administrator.myapplication.find.billboard.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentBillboardBinding;
import com.example.administrator.myapplication.find.billboard.bean.BillBoardEntity.BillBoardData;
import com.example.administrator.myapplication.find.billboard.bean.BillBoardEntity.BillBoardEntity;
import com.example.administrator.myapplication.find.billboard.bean.BillBoardEntity.BillBoardTitle;
import com.example.administrator.myapplication.find.billboard.bean.BillBoardEntity.BillBoardTop;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BillboardFragment extends Fragment implements Callback<BillBoardEntity> {
     private FragmentBillboardBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;

    public BillboardFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return DataBindingUtil.inflate(inflater,R.layout.fragment_billboard, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding= DataBindingUtil.getBinding(view);
        HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(BillBoardTop.class,Pair.create(R.layout.billboard_image_top, BR.billBoardTop));
        map.put(BillBoardData.class,Pair.create(R.layout.billboard_title_top, BR.billBoardData1));
        map.put(BillBoardTitle.class,Pair.create(R.layout.billboard_item, BR.billBoardTitle));
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(),map);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
       service.getBillBoardEntity().enqueue(this);
    }

    @Override
    public void onResponse(Call<BillBoardEntity> call, Response<BillBoardEntity> response) {
        BillBoardTop billBoardTop = response.body().getFocusImages().getList().get(0);
        adapter.add(0,billBoardTop);
        BillBoardData billBoardData = response.body().getDatas().get(0);
        adapter.add(1,billBoardData);
        adapter.addAll(billBoardData.getList());
        BillBoardData billBoardData1 = response.body().getDatas().get(1);
        adapter.add(billBoardData1);
        adapter.addAll(billBoardData1.getList());

    }

    @Override
    public void onFailure(Call<BillBoardEntity> call, Throwable t) {
        t.getMessage();
//        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
